package site.sorghum.ddl.dialect;

import site.sorghum.ddl.entity.DdlColumnWrap;
import site.sorghum.ddl.enums.DbType;
import site.sorghum.ddl.field.MysqlTypeConvert;
import site.sorghum.ddl.field.TypeConverter;

public class MySQLDDLDialect extends AbstractDDLDialect {

    @Override
    public DbType getDatabaseType() {
        return DbType.MYSQL;
    }

    @Override
    public TypeConverter typeConverter() {
        return MysqlTypeConvert.INSTANCE;
    }

    @Override
    protected String quoteIdentifier(String identifier) {
        return "`" + identifier + "`";
    }

    @Override
    protected String quoteValue(Object value) {
        if (value instanceof String) {
            return "'" + ((String) value).replace("'", "''") + "'";
        }
        return String.valueOf(value);
    }

    @Override
    protected String getColumnCommentClause(DdlColumnWrap column) {
        if (column.getRemarks() != null && !column.getRemarks().isEmpty()) {
            return " COMMENT " + quoteValue(column.getRemarks());
        }
        return "";
    }

    @Override
    protected String getDefaultValueClause(DdlColumnWrap column) {
        if (column.getDefaultValue() != null && !column.getDefaultValue().isEmpty()) {
            return " DEFAULT " + quoteValue(column.getDefaultValue());
        }
        return "";
    }


}